﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BodyFormat
{
    class FormData
    {
        private List<String> format = new List<String>();

        bool isflies = false;

        public string build(IEnumerator en)
        {
            buildNode(en);
            string s = "";
            int it = 0;
            foreach (string i in format)
            {
                if (it == 0)
                {
                    if (format.Count > 1)
                    {
                        s += i + "\r\n";
                    }
                }
                else {
                    s += i + "\r\n";
                }
                it += 1;
            }
            if (isflies)
            {
                return s;
            }
            else {
                return "";
            }
            
        }

        private void buildNode(IEnumerator en)
        {
            while (en.MoveNext())
            {
                //获取转换当前节点数据类型
                DevExpress.XtraTreeList.Nodes.TreeListNode node = (DevExpress.XtraTreeList.Nodes.TreeListNode)en.Current;
                if (node.Nodes.Count > 0)
                {
                    buildNode(node.Nodes.GetEnumerator());
                }
                else
                {
                    string ss = "";
                    string type = (string)node.GetValue("Type");
                    if (type == "File")
                    {
                        isflies = true;
                        ss = @"FormData.setFile(""" + node.GetValue("Key") + @""",""" + node.GetValue("Value") + @""",""" + node.GetValue("Key") + @""");";
                    }
                    if (type != "File" && type != "Group")
                    {
                        ss = @"FormData.setText(""" + node.GetValue("Key") + @""",""" + node.GetValue("Value") + @""");";
                    }
                    format.Add(ss);
                }
            }
        }

    }
}
